package top.someapp.dxf2dsvg.dxf;

import top.someapp.dxf2dsvg.base.DxfException;

import javax.annotation.Nonnull;

/**
 * @author zwz
 * Created on 2020-04-17
 */
public class Sections {
    private Sections() {
        // prevent outer new instance.
    }

    public static Section parse(@Nonnull DxfReader reader, @Nonnull Item item) {
        Section section;
        Section.SectionName sectionName = Enum.valueOf(Section.SectionName.class, item.value);
        switch (sectionName) {
            case HEADER:
                section = new SectionHeader();
                break;
            case CLASSES:
                section = new SectionClasses();
                break;
            case TABLES:
                section = new SectionTables();
                break;
            case BLOCKS:
                section = new SectionBlocks();
                break;
            case ENTITIES:
                section = new SectionEntities();
                break;
            case OBJECTS:
                section = new SectionObjects();
                break;
            case THUMBNAILIMAGE:
                section = new SectionThumbnailImage();
                break;
            default:
                throw new DxfException("Unknown item: " + item);
        }

        for (Item next; ; ) {
            next = reader.next();
            if ("ENDSEC".equals(next.value))
                break;
            section.onItemFound(next);
        }
        return section;
    }
}
